package com.tgpms.web.element.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tgpms.common.PageUtils;
import com.tgpms.common.Query;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.element.dao.WsQualityDisputeMapper;
import com.tgpms.web.element.entity.WsQualityDispute;
import com.tgpms.web.element.service.IWsQualityDisputeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 质量争议问题 服务实现类
 * </p>
 *
 * @author jl
 * @since 2020-07-08
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class WsQualityDisputeServiceImpl implements IWsQualityDisputeService {

	@Resource
	private WsQualityDisputeMapper mapper;
	
	/**
		新增质量争议问题
	*/
	@Override
	public Result addWsQualityDispute(WsQualityDispute en,Result result) {
		String id = Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		en.setId(id);
		en.setRowstamp(id);
		en.setInsertDate(new Date());
		int i = mapper.insert(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(id);
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	/**
		更新质量争议问题
	*/
	@Override
	public Result updateWsQualityDispute(WsQualityDispute en,Result result) {
		int i = mapper.updateById(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	/**
		根据主键删除质量争议问题
	*/
	@Override
	public Result deleteWsQualityDispute(Serializable id,Result result) {
		int i = mapper.deleteById(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

    @Override
    public Result selWsQualityDisputeList(Map<String, Object> params) {
		String key = (String) params.get("key");
		QueryWrapper<WsQualityDispute> wrapper = new QueryWrapper<>();
		if(!StringUtils.isEmpty(key)){
			wrapper.and((obj)->{
				obj.like("PO_NO",key).or().like("ISSUE",key);
			});
		}
		IPage<WsQualityDispute> page = this.mapper.selectPage(
				new Query<WsQualityDispute>().getPage(params),
				wrapper
		);
		PageUtils pageUtils = new PageUtils(page);
		Result r = new Result();
		r.setData(pageUtils);
		return r;
    }
}
